//+------------------------------------------------------------------------------------------------------------------+
#property description                                                                                "[XU-SEMAFOR v3]"
#define Version                                                                                      "[XU-SEMAFOR v3]"
//+------------------------------------------------------------------------------------------------------------------+
#property link        "https://forex-station.com/viewtopic.php?p=1295409935#p1295409935"
#property description "THIS IS A FREE INDICATOR"
#property description "                                                      "
#property description "Welcome to XARD UNIVERSE"
#property description "Let light shine out of darkness and illuminate your world"
#property description "and with this freedom leave behind your cave of denial"
#property indicator_chart_window
#property indicator_buffers 10
extern string Indicator                  = Version;  string ID;  extern int maxBars=5000;
//+------------------------------------------------------------------------------------------------------------------+
extern string STR01                      = "<<<==== [01] SEMA Settings ====>>>";
  extern bool showSEMA1                  = true,showSEMA2=true,showSEMA3=true,showSEMA4=true,showSEMA5=true;
//+---XARD SETTINGS(1008,144,36,9,144)-------------------------------------------------------------------------------+
extern double DotPer1                    = 1008,DotPer2=144,DotPer3=36,DotPer4=9,DotPer5=144,deltazz,myPoint;
       string Dev_Step_1                 = "0,5",Dev_Step_2="0,5",Dev_Step_3="0,5",Dev_Step_4="0,5",Dev_Step_5="0,5";
          int S1size                     = 16,S2size=12,S3size=9,S4size=4,S5size=7;
          int S1kod                      = 110,S2kod=110,S3kod=108,S4kod=108,S5kod=221,S6kod=222;
        color cU1=clrBlue,cD1=clrGold,
              cU2=clrLimeGreen,cD2=clrCrimson,
              cU3=clrSnow,cD3=clrSnow,
              cU4=clrLimeGreen,cD4=clrCrimson,
              cU5=clrSnow,cD5=clrSnow;
          int APer,BPer,CPer,DPer,EPer,Dev1,Stp1,Dev2,Stp2,Dev3,Stp3,Dev4,Stp4,Dev5,Stp5,Type;
       double ABufUp[],ABufDn[],BBufUp[],BBufDn[],CBufUp[],CBufDn[],DBufUp[],DBufDn[],EBufUp[],EBufDn[];
//+----OnInit Function-----------------------------------------------------------------------------------------------+
   int OnInit(){int Buff=10,Buf=-1; IndicatorBuffers(Buff);
   ID = "m"; IndicatorDigits(Digits); IndicatorShortName(ID);
//+------------------------------------------------------------------------------------------------------------------+
   if(DotPer1>0) APer=MathCeil(DotPer1*Period()); else APer=0;
   if(DotPer2>0) BPer=MathCeil(DotPer2*Period()); else BPer=0;
   if(DotPer3>0) CPer=MathCeil(DotPer3*Period()); else CPer=0;
   if(DotPer4>0) DPer=MathCeil(DotPer4*Period()); else DPer=0;
   if(DotPer5>0) EPer=MathCeil(DotPer5*Period()); else EPer=0;
//+---GOLD SQUARE----------------------------------------------------------------------------------------------------+
   if(showSEMA1)    Type=DRAW_ARROW; else Type=DRAW_NONE;
   if(DotPer1>0){
   Buf+=1; SetIndexBuffer(Buf,ABufUp); SetIndexStyle(Buf,Type,0,S1size,cU1);
           SetIndexArrow(Buf,S1kod);   SetIndexEmptyValue(Buf,0.0);
   Buf+=1; SetIndexBuffer(Buf,ABufDn); SetIndexStyle(Buf,Type,0,S1size,cD1);
           SetIndexArrow(Buf,S1kod);   SetIndexEmptyValue(Buf,0.0);}
//+---PINK DOT-------------------------------------------------------------------------------------------------------+
   if(showSEMA3)    Type=DRAW_ARROW; else Type=DRAW_NONE;
   if(DotPer3>0){
   Buf+=1; SetIndexBuffer(Buf,CBufUp); SetIndexStyle(Buf,Type,0,S3size,cU3);
           SetIndexArrow(Buf,S3kod);   SetIndexEmptyValue(Buf,0.0);
   Buf+=1; SetIndexBuffer(Buf,CBufDn); SetIndexStyle(Buf,Type,0,S3size,cD3);
           SetIndexArrow(Buf,S3kod);   SetIndexEmptyValue(Buf,0.0);}
//+---SILVER DOT-----------------------------------------------------------------------------------------------------+
   if(showSEMA4)    Type=DRAW_ARROW; else Type=DRAW_NONE;
   if(DotPer4>0){
   Buf+=1; SetIndexBuffer(Buf,DBufUp); SetIndexStyle(Buf,Type,0,S4size,cU4);
           SetIndexArrow(Buf,S4kod);   SetIndexEmptyValue(Buf,0.0);
   Buf+=1; SetIndexBuffer(Buf,DBufDn); SetIndexStyle(Buf,Type,0,S4size,cD4);
           SetIndexArrow(Buf,S4kod);   SetIndexEmptyValue(Buf,0.0);}
//+---DODGERBLUE TURNING POINTS--------------------------------------------------------------------------------------+
   if(showSEMA2)    Type=DRAW_ARROW; else Type=DRAW_NONE;
   if(DotPer2>0){
   Buf+=1; SetIndexBuffer(Buf,BBufUp); SetIndexStyle(Buf,Type,0,S2size,cU2);
           SetIndexArrow(Buf,S2kod);   SetIndexEmptyValue(Buf,0.0);
   Buf+=1; SetIndexBuffer(Buf,BBufDn); SetIndexStyle(Buf,Type,0,S2size,cD2);
           SetIndexArrow(Buf,S2kod);   SetIndexEmptyValue(Buf,0.0);}
//+---ARROW DOT------------------------------------------------------------------------------------------------------+
   if(showSEMA5)    Type=DRAW_ARROW; else Type=DRAW_NONE;
   if(DotPer5>0){
   Buf+=1; SetIndexBuffer(Buf,EBufUp); SetIndexStyle(Buf,Type,0,S5size,cU5);
           SetIndexArrow(Buf,S5kod);   SetIndexEmptyValue(Buf,0.0);
   Buf+=1; SetIndexBuffer(Buf,EBufDn); SetIndexStyle(Buf,Type,0,S5size,cD5);
           SetIndexArrow(Buf,S6kod);   SetIndexEmptyValue(Buf,0.0);}
//+------------------------------------------------------------------------------------------------------------------+
   int CDev=0,CSt=0,Mass[],C=0;
   if(IntFromStr(Dev_Step_1,C,Mass)==1){Stp1=Mass[1]; Dev1=Mass[0];}
   if(IntFromStr(Dev_Step_2,C,Mass)==1){Stp2=Mass[1]; Dev2=Mass[0];}
   if(IntFromStr(Dev_Step_3,C,Mass)==1){Stp3=Mass[1]; Dev3=Mass[0];}
   if(IntFromStr(Dev_Step_4,C,Mass)==1){Stp4=Mass[1]; Dev4=Mass[0];}
   if(IntFromStr(Dev_Step_5,C,Mass)==1){Stp5=Mass[1]; Dev5=Mass[0];}
//+------------------------------------------------------------------------------------------------------------------+
   if(Buff != Buf+1) Print("*******Buffer MisMatch!!!   ",Buff," ",Buf);
   for(int Bufx=0;Bufx<indicator_buffers;Bufx++){SetIndexLabel(Bufx,NULL);}  return(INIT_SUCCEEDED);}//End OnInit
//+----deinit Function-----------------------------------------------------------------------------------------------+
   int deinit(){CleanUpOnIsle9();  return(0);}
//+----OnCalculate Function------------------------------------------------------------------------------------------+
   int OnCalculate(const int rates_total,
                   const int prev_calculated,
                   const datetime &time[],
                   const double &open[],
                   const double &high[],
                   const double &low[],
                   const double &close[],
                   const long &tick_volume[],
                   const long &volume[],
                   const int &spread[]){
//+------------------------------------------------------------------------------------------------------------------+
   int limit,counted_bars=IndicatorCounted(); if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--; limit=Bars-counted_bars; if(limit>maxBars){limit=maxBars;}
//+------------------------------------------------------------------------------------------------------------------+
   if(DotPer1>0) CountZZ(ABufUp,ABufDn,DotPer1,Dev1,Stp1);
   if(DotPer2>0) CountZZ(BBufUp,BBufDn,DotPer2,Dev2,Stp2);
   if(DotPer3>0) CountZZ(CBufUp,CBufDn,DotPer3,Dev3,Stp3);
   if(DotPer4>0) CountZZ(DBufUp,DBufDn,DotPer4,Dev4,Stp4);
   if(DotPer5>0) CountZZ(EBufUp,EBufDn,DotPer5,Dev5,Stp5);
//+------------------------------------------------------------------------------------------------------------------+
   return(rates_total);}//End OnCalculate
//+----Clean Chart Function------------------------------------------------------------------------------------------+
   void CleanUpOnIsle9(){string namem; for(int m=ObjectsTotal()-1; m>=0; m--){namem=ObjectName(m);
   if(StringSubstr(namem,0,StringLen(ID))==ID) {ObjectDelete(namem);}}}//EOF
//+----END OF FILE---------------------------------------------------------------------------------------------------+
   int CountZZ(double& ExtMapBufferUp[],double& ExtMapBufferDn[],int Depth,int ExtDeviation,int ExtBackstep){
   int shiftZZ,back,lasthighpos,lastlowpos; double val,res,curlow,curhigh,lasthigh,lastlow;
   for(shiftZZ=Bars-Depth; shiftZZ>=0; shiftZZ--){val=Low[iLowest(NULL,0,MODE_LOW,Depth,shiftZZ)];
   if(val==lastlow) val=0.0;  else { lastlow=val;
   if((Low[shiftZZ]-val)>(ExtDeviation*myPoint)) val=0.0;  else {
   for(back=1; back<=ExtBackstep; back++){res=ExtMapBufferUp[shiftZZ+back];
   if (res!=0.0) res = res+deltazz;
   if((res!=0.0)&&(res>val)) ExtMapBufferUp[shiftZZ+back]=0.0;}}}
   if(val==0.0) ExtMapBufferUp[shiftZZ]=0.0; else ExtMapBufferUp[shiftZZ]=val-deltazz;
//--- high
   val=High[iHighest(NULL,0,MODE_HIGH,Depth,shiftZZ)];
   if(val==lasthigh) val=0.0;  else { lasthigh=val;
   if((val-High[shiftZZ])>(ExtDeviation*myPoint)) val=0.0; else {
   for(back=1; back<=ExtBackstep; back++){ res=ExtMapBufferDn[shiftZZ+back];
   if(res!=0.0) res = res-deltazz;
   if((res!=0.0)&&(res<val)) ExtMapBufferDn[shiftZZ+back]=0.0;}}}
   if(val==0.0) ExtMapBufferDn[shiftZZ]=0.0; else ExtMapBufferDn[shiftZZ]=val+deltazz;}
//--- final cutting
   lasthigh=-1;  lasthighpos=-1;  lastlow=-1;  lastlowpos=-1;
   for(shiftZZ=Bars-Depth; shiftZZ>=0; shiftZZ--){ curlow=ExtMapBufferUp[shiftZZ];
   if(curlow != 0.0) curlow = curlow+deltazz;    curhigh=ExtMapBufferDn[shiftZZ];
   if(curhigh != 0.0) curhigh = curhigh-deltazz;
   if((curlow==0.0)&&(curhigh==0.0)) continue;
//---
   if(curhigh!=0.0){if(lasthigh>0.0){
   if(lasthigh<curhigh) ExtMapBufferDn[lasthighpos]=0.0; else ExtMapBufferDn[shiftZZ]=0.0;}
//---
   if(lasthigh<curhigh || lasthigh<0.0){lasthigh=curhigh; lasthighpos=shiftZZ;} lastlow=-1;}
   if(curlow!=0.0){if(lastlow>0.0){
   if(lastlow>curlow) ExtMapBufferUp[lastlowpos]=0.0; else ExtMapBufferUp[shiftZZ]=0.0;}
//---
   if((curlow<lastlow)||(lastlow<0.0)){lastlow=curlow; lastlowpos=shiftZZ;} lasthigh=-1;}}
   for(shiftZZ=Bars-1; shiftZZ>=0; shiftZZ--){
   if(shiftZZ>=Bars-Depth) ExtMapBufferUp[shiftZZ]=0.0; else {break;}} return(0);}
//+------------------------------------------------------------------------------------------------------------------+
   int Str2Massive(string VStr,int& M_Count,int& VMass[]){int val=StrToInteger(VStr); if(val>0){M_Count++;
   int mc=ArrayResize(VMass,M_Count); if(mc==0)return(-1); VMass[M_Count-1]=val; return(1);} else return(0);}
//+------------------------------------------------------------------------------------------------------------------+
   int IntFromStr(string ValStr,int& M_Count, int& VMass[]){if(StringLen(ValStr)==0) return(-1);
   string SS=ValStr; int NP=0; string CS; M_Count=0; ArrayResize(VMass,M_Count);
   while(StringLen(SS)>0){NP=StringFind(SS,",");
   if(NP>0){CS=StringSubstr(SS,0,NP); SS=StringSubstr(SS,NP+1,StringLen(SS));}  else {
   if(StringLen(SS)>0){CS=SS; SS="";}} if(Str2Massive(CS,M_Count,VMass)==0){return(-2);}} return(1);}
//+------------------------------------------------------------------------------------------------------------------+